package com.cuckoo.cuckoo_app2.apiAsync.Imp;

import android.util.Log;

import com.cuckoo.cuckoo_app2.apiAsync.BaseCallbac;
import com.cuckoo.cuckoo_app2.apiAsync.VersionControlApi;
import com.cuckoo.cuckoo_app2.config.ApiConfig;
import com.cuckoo.cuckoo_app2.utils.NetworkRequestHelperUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class VersionControlApiImp implements VersionControlApi {

    private Gson gson;
    private ApiConfig apiConfig;

    public VersionControlApiImp() {
        gson = new Gson();
        this.apiConfig = new ApiConfig();
    }

    @Override
    public void VersionListAsync(BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuVersionControl/gatMap";
        new Thread(new Runnable() {
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "GET:" + http);
                try {
                    Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                    String response = NetworkRequestHelperUtils.get(http, headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();

    }

    @Override
    public void VipListAsync(BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuVipCard/vipList";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "GET:" + http);
                try {
                    Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                    String response = NetworkRequestHelperUtils.get(http, headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void CommissionListAsync(BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuCommissionManagement/getAll";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);
                try {
                    Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                    String response = NetworkRequestHelperUtils.get(http, headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void BuyVipCardAsync(String cardId, BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuVipCard/buyVipCard";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Map<String, String> body = new HashMap<>();
                body.put("id", cardId);
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.postJson(http, gson.toJson(body), headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();

    }
}
